package com.shuyou;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import com.shuyou.lib.BookOwnersListView;
import com.shuyou.lib.BookOwnersListViewAdapter;
import com.shuyou.net.Data;
import com.shuyou.net.JsonParser;
import com.shuyou.net.ServerInfo;
import com.shuyou.net.StaticHttpClient;
import com.shuyou.net.User;
import com.shuyou.values.Values;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

public class BookOwnersActivity extends Activity{

	private Dialog dialogLoading;
	private HashMap<String, String> map = null;
	private Data data = null;
	private static final String URL = "http://shuyou.duapp.com/api/Book/isbnUsers";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_bookowners);
		
		findViewById(R.id.bookowners_back).setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		LinearLayout dialogUsrInfoView = (LinearLayout)getLayoutInflater()
				.inflate(R.layout.shuyou_dialog_loading, null);
		dialogLoading = new Dialog(BookOwnersActivity.this, R.style.ShuyouDialog);
		dialogLoading.setContentView(dialogUsrInfoView);
		dialogLoading.show();
		dialogLoading.setCancelable(false); //弹窗不能被关闭
		
		Intent i = getIntent();
		isbnUsers(i.getStringExtra("isbn"));
	}
	
	private void isbnUsers(final String isbn) {
		map = new HashMap<String, String>();
		//TODO
		map.put("test", "1");
		map.put("isbn", isbn);
		
		new Thread() {

			@Override
			public void run() {
				try {
					String strResult = StaticHttpClient.doGet(URL, map);
					if (strResult == null) {
						System.out.println("null");
					} else {
						Log.i("BookOwnersActivity", strResult);
					}
					ServerInfo si = (ServerInfo) JsonParser.parseObject(strResult,
							ServerInfo.class);//需要处理空指针异常
					System.out.println(si);
					String status = si.getStatus();
					if(status.equals("10000"))//操作正确
					{
						data = si.getData();
						Message msg = new Message();
						msg.what = 0;
						msg.obj = data;
						handler.sendMessage(msg);
						
					}
					else if(status.equals("10003"))//参数错误
					{
						//TODO sth.
						Message msg = new Message();
						msg.what = 1;
						handler.sendMessage(msg);
					}
					
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					Message msg = new Message();
					msg.what = Values.NETWORK_ERROR;
					handler.sendMessage(msg);
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					Message msg = new Message();
					msg.what = Values.NETWORK_ERROR;
					handler.sendMessage(msg);
					e.printStackTrace();
				} catch (Exception e) {
					Message msg = new Message();
					msg.what = Values.NETWORK_ERROR;
					handler.sendMessage(msg);
					e.printStackTrace();
				}
			}

		}.start();

	}
	
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case 0:
				// TODO Auto-generated method stub
				List<BookOwnersListView> bookOwners = new ArrayList<BookOwnersListView>();
				ListView list = (ListView)findViewById(R.id.list_bookowners);
				data = (Data)msg.obj;
				if (data != null) {
					//最好判断一下获取的Sessionid是否为空串或者null
					ArrayList<User> userlist = data.getUser_list();
					System.out.println(userlist);
					for (User ib : userlist) {
						BookOwnersListView bookOwner = new BookOwnersListView(ib.getHead_pic(), ib.getNick_name(), 
								ib.getTel(), ib.getQq(), ib.getMic_msg(), ib.getComment());
						bookOwners.add(bookOwner);
					}
					BookOwnersListViewAdapter listAdapter = new BookOwnersListViewAdapter(BookOwnersActivity.this, bookOwners, list);
					list.setAdapter(listAdapter);
				}
				dialogLoading.cancel();
				break;
			case Values.NETWORK_ERROR:
				Toast.makeText(BookOwnersActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
				dialogLoading.cancel();
				break;
			}
		}
		
	};

}
